#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
bool vis[maxn];
LL match[maxn];
pair<LL,LL>p[maxn];
vector<LL>g[maxn];
bool dfs(LL u){
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(vis[v]) continue;
        vis[v]=true;
        if(match[v]==0||dfs(match[v])){
            match[v]=u;
            vis[v]=false;
            return true;
        }
    }
    return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=m;i++){
     LL num;cin>>num;
     if(num==1) {LL x;cin>>x;g[i].push_back(x);LL val;cin>>val;p[i].first=val,p[i].second=i;};
     if(num==2) {LL x,y;cin>>x>>y;g[i].push_back(x);g[i].push_back(y);LL val;cin>>val;p[i].first=val;p[i].second=i;}
  }
  sort(p+1,p+1+m);
  LL ans=0;
  for(LL i=m;i>=0;i--){
     if(dfs(p[i].second)) ans+=p[i].first;
  }
  cout<<ans<<"\n";
return 0;
}
